package cn.com.backstage.controller.base;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Date;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.ApiT_tags;

import cn.com.api.service.ApiT_tagsService;

import cn.com.util.*;
import cn.com.util.*;


public class T_tagsBaseController {

	private static Logger logger = Logger.getLogger(T_tagsBaseController.class);
	
	@Autowired
	private ApiT_tagsService apiT_tagsService;
	
	/**
	 * 列表
	 * @return
	 */
	@RequestMapping(value = "t_tags-list")
	public ModelAndView t_tags_list(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = new ModelAndView("forward:/backstage/t_tags_list.jsp");
		
		List<ApiT_tags> t_tagss = apiT_tagsService.findT_tagss();
		mv.addObject("t_tagss", t_tagss);
		
		return mv;
	}
	
	/**
	 * 进入新增或者编辑页面
	 * @return
	 */
	@RequestMapping(value = "t_tags-add-pre")
	public ModelAndView t_tags_add_pre(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = new ModelAndView("forward:/backstage/t_tags_add.jsp");
		//
		if(request.getParameter("id")!=null){
			String id = request.getParameter("id");
			ApiT_tags t_tags = apiT_tagsService.findT_tagsById(Integer.valueOf(id));
			mv.addObject("t_tags",t_tags);
		}
		return mv;
	}

	/**
	 *新增或者编辑
	 * @return
	 */
	@RequestMapping(value = "t_tags-add")
	public void t_tags_add(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String tag_id = request.getParameter("tag_id");
		String tag = request.getParameter("tag");
		String user_id = request.getParameter("user_id");
		String count = request.getParameter("count");
		String is_hot = request.getParameter("is_hot");
		
		Map<String,Object> map = new HashMap<String,Object>();
		String id = request.getParameter("id");
		//add
		if(id==null || (id.trim()).equals("")){
			ApiT_tags t_tags = new ApiT_tags();
           
		    //t_tags.setTag_id(tag_id);
		    t_tags.setTag(tag);
		    //t_tags.setUser_id(user_id);
		    t_tags.setCount(Integer.valueOf(count));
		    t_tags.setIs_hot(Integer.valueOf(is_hot));
	        
			apiT_tagsService.insertT_tags(t_tags);
		}
		//edit
		else{
			ApiT_tags t_tags = apiT_tagsService.findT_tagsById(Integer.valueOf(id));
	      
		    //t_tags.setTag_id(tag_id);
		    t_tags.setTag(tag);
		    //t_tags.setUser_id(user_id);
		    t_tags.setCount(Integer.valueOf(count));
		    t_tags.setIs_hot(Integer.valueOf(is_hot));
			
			apiT_tagsService.updateT_tags(t_tags);
		}
		//
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "添加成功！");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}
	
	/**
	 *删除
	 * @return
	 */
	@RequestMapping(value = "t_tags-delete")
	public void t_tags_delete(HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		Map<String,Object> map = new HashMap<String,Object>();
		String id = request.getParameter("id");
		
		//delete
		int ret_i =	apiT_tagsService.deleteT_tags(Integer.valueOf(id));
		
		//
		if(ret_i > 0){
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "删除成功！");
		}
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}
	
}



